package com.example.valueanimationdemo.slice;

import com.example.valueanimationdemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Text;

public class MainAbilitySlice extends AbilitySlice implements Animator.StateChangedListener, Animator.LoopedListener {
    // 定义AnimatorValue实例对象
    private AnimatorValue animatorValue = new AnimatorValue();
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        // 获取模板中自动生成的Text组件实例
        Text text = (Text) findComponentById(ResourceTable.Id_text_helloworld);
        // 设置动画的执行时长，单位为毫秒
        animatorValue.setDuration(10000);
        // 设置开启后，延迟多久开始执行动画，单位毫秒
        animatorValue.setDelay(0);
        // 设置播放动画的次数
        animatorValue.setLoopedCount(AnimatorValue.INFINITE);
        // 设置动画播放的时间函数
        animatorValue.setCurveType(Animator.CurveType.LINEAR);
        // 设置数值变化监听
        animatorValue.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                // 在监听的回调方法中，修改Text组件的文本，从而实现动画效果
                text.setText(String.format("倒计时：%.2f秒", 10 - (10 * v)));
            }
        });
        // 开启动画
        animatorValue.start();
    }

    @Override
    public void onStart(Animator animator) {
        // 动画开始
    }

    @Override
    public void onStop(Animator animator) {

    }

    @Override
    public void onCancel(Animator animator) {

    }

    @Override
    public void onEnd(Animator animator) {

    }

    @Override
    public void onPause(Animator animator) {

    }

    @Override
    public void onResume(Animator animator) {

    }

    @Override
    public void onRepeat(Animator animator) {

    }
}
